<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   BKPay
 * @copyright  Copyright (c) 2011 bkpay.vn
 * @license    http://license.bkpay.vn
 * @version    v 1.0 2011-09-19
 * @author	   hoaihuong@9webdesigns.com, s50702790@gmail.com
 */

require_once 'BKPay/Soap/Server.php';
require_once 'BKPay/Soap/AutoDiscover.php';
require_once 'BKPay/Exception.php';
require_once 'services/PayService.php';
require_once 'Zend/Soap/Wsdl/Strategy/ArrayOfTypeComplex.php';
require_once 'Zend/Soap/Wsdl/Strategy/ArrayOfTypeSequence.php';

class transaction_ServerController extends Nine_Controller_Action
{
	private $_WSDL_URI = "https://bkpay.local/transaction/server/api?wsdl";
	
	public function apiAction()
	{
		/** 
		 * Set layout and disable view renderers
		 */
//		ini_set('soap.wsdl_cache_enabled', 0);
		$this->setLayout('default');
		$this->getHelper ( 'viewRenderer' )->setNoRender ( true );
//		$this->getResponse()->sendHeaders('Content-type','text/xml');
		if(isset($_GET['wsdl'])) {
    		//return the WSDL
    		$this->hadleWSDL();
		} else {
			//handle SOAP request
    		$this->handleSOAP();
		}
	}
	
	
	private function hadleWSDL() {
		$autodiscover = new BKPay_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
//		$autodiscover->setBindingStyle(array('style'=>'document'));
//    	$autodiscover->setOperationBodyStyle(array('use' => 'literal'));
    	$autodiscover->setClass('PayService');
    	$autodiscover->handle();
    	
    	
	}
    
	private function handleSOAP() {
		
		$session = Nine_Registry::getSession();
		
		$soap = new BKPay_Soap_Server($this->_WSDL_URI); 
    	$soap->setClass('PayService');
//    	$soap->setWsdlCache(0);
    	$soap->registerFaultException(array('BKPay_Exception'));
    	$soap->setPersistence(SOAP_PERSISTENCE_SESSION);
    	$soap->handle();
	}
}
